// JScript source code
/*
1. Write a function spellcheck that returns an array. The first element
will be the word to check. The subsequent elements will be the suggestions.
This function should return an array of arrays. The subarrays will contain the word
to check and suggestions. If the subarray contains only one element the word is spelled
correctly or cannot be checked by the engine.

2. Write a function that walks a XML document. It must split the document into
single words. For example it can get the text of an element then the text can be
easily split into an array of words. Each word can be spellchecked, then the correct
word and suggestions can be returned if necessary.
arrWords = eleXML.getText().split() //get the text into an array
for each arrWords
    arrChecked = spellcheck(arrWords[i])
next

OR

Can SAX2 be used and just walk through the document event-wise as suggested:

start document
start element: doc
start element: para
characters: Hello, world!
end element: para
end element: doc
end document


<myxml> 
<p>You really think you are a good speler, don't you?</p> 
</myxml> 


var xmlDocument = new ActiveXObject('Msxml2.DOMDocument.3.0'); 
xmlDocument.loadXML([ 
   '<myxml>', 
   '<p>You really think you are a good speler, don\'t you?</p>', 
   '</myxml>' 
].join('\r\n')); 
xmlDocument.setProperty('SelectionLanguage', 'XPath'); 
var p = xmlDocument.selectSingleNode('myxml/p'); 
var currentChild = p.firstChild; 
var newChild = currentChild.splitText(currentChild.data.indexOf('speler')); 
newChild.splitText('speler'.length); 
var newParent = xmlDocument.createElement('sp'); 
newChild.parentNode.replaceChild(newParent, newChild); 
newParent.appendChild(newChild); 
var sug = xmlDocument.createElement('sug'); 
sug.appendChild(xmlDocument.createTextNode('speller')); 
newParent.appendChild(sug); 


the XML markup (xmlDocument.xml) is now 


<myxml> 
        <p>You really think you are a good <sp>speler<sug>speller</sug></sp>, 
don't you?</p> 
</myxml> 




*/
xmlSpellcheck();

/******************************************************************************
function: xmlSpellcheck
description: Check the spelling of an entire XML document using SAX2.
input:
    An XML document to check.
output:
    A checked XML document to save to disk.
******************************************************************************/
function xmlSpellcheck()
{
    WScript.Echo("Here!");
    var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
    var comment;
    var root;
    var arrText;
    var arrWords;
    xmlDoc.async = false;
    xmlDoc.load("../xml/00000001.xml");
    if (xmlDoc.parseError.errorCode != 0)
    {
       var myErr = xmlDoc.parseError;
       WScript.Echo("Error: " + myErr.reason);
    }
    else
    {
        //create word object and add document
        var objWord = new ActiveXObject("Word.Application");
        var objDoc = objWord.Documents.Add();
        
       root = xmlDoc.documentElement;
       for (var i=0; i<root.childNodes.length; i++)
       {
          //if (root.childNodes.item(i).nodeType == 8)
          //{
          WScript.Echo(i);
             comment = root.childNodes.item(i);
             //WScript.Echo(comment.text);
             arrText = comment.text.split(" ");
             for(var j=0; j<arrText.length; j++)
             {
                arrWords = spellcheck(arrText[j], objWord);
                for(var k=0; k<arrWords.length; k++)
                {
                    if(k==0)
                    {
                        WScript.Echo('<item>' + arrWords[k] + '</item>');
                    }
                    
                }
             }
          //}
       }
       objWord.Quit();
    }
}
/******************************************************************************
function: xmlWalk
description: Walk an XML document.
input:
    An XML root element to walk
output:
    Child nodes of the given XML.
******************************************************************************/
function getNodeList(xmlNode)
{

}

/******************************************************************************
function: spellcheck
description: Check the spelling of a single word and return the word and it's
suggested spellings.
input:
    strWord: A string to spell check
    objWord: An ActiveXObject for the Microsoft Word application.
output:
    arrWords: An array of strings. The first element is the word to check.
    Subsequent elements are the suggestions, if necessary.
******************************************************************************/
function spellcheck(strWord, objWord)
{
    var arrWords = new Array();
    //var objWord = new ActiveXObject("Word.Application");
    //var objDoc = objWord.Documents.Add();
    //var strInput;
    //do 
    //{
        //WScript.StdOut.Write("Please enter the word to be checked: "); 

        
        //strInput = WScript.StdIn.ReadLine();

        //WScript.Echo("strInput: " + strInput);
        /*if(strInput == "")
        {
            WScript.Echo("I Quit!");
            break;
        }*/
        //WScript.Echo(strInput);
        if(!objWord.CheckSpelling(strWord))
        {
        //    WScript.Echo("The word is spelled correctly.");
        //}
        //else
        //{
        //    WScript.Echo("The word is not spelled correctly.");
            arrWords[0] = strWord;
            colSuggestions = objWord.GetSpellingSuggestions(strWord);
            
            for(i = 1; i <= colSuggestions.count; i++)
            {
                //strSuggestion = colSuggestions.item(i);
                //WScript.Echo(i + ": " + strSuggestion);
                arrWords[i] = colSuggestions.item(i);
            }
        } 
    //}
    //while(true);


    //objWord.Quit();
    return arrWords;
}